package com.huidao.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.huidao.entity.Goods;
import com.huidao.entity.Shoppingcart;
import com.huidao.entity.User;
import com.huidao.util.JDBCUtil;

public class ShoppingcartDaoImpl implements ShoppingcartDao {
	
	Connection conn = null;
	PreparedStatement ps =null;
	ResultSet rs =null;
	
	@Override
	public boolean addShoppingcart(Shoppingcart shoppingcart) {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO SHOPPINGCART(USER_ID,GOODS_ID,GOODS_NUM) VALUES(?,?,?)";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, shoppingcart.getUser().getUserId());
			ps.setLong(2,shoppingcart.getGoods().getGoodsId() );
			ps.setLong(3, shoppingcart.getGoodsNum());
			int i  =ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	@Override
	public boolean delShoppingcart(Long id) {
		// TODO Auto-generated method stub
		String sql = "DELETE FROM SHOPPINGCART WHERE SHOPPINGCART_ID=?";
		
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, id);
			int i  =ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}
	

	@Override
	public boolean delAllShoppingcart() {
		// TODO Auto-generated method stub
		String sql = "DELETE FROM SHOPPINGCART";
		
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			int i  =ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	@Override
	public boolean updateShoppingcart(Shoppingcart shoppingcart) {
		// TODO Auto-generated method stub
		String sql = "UPDATE SHOPPINGCART SET GOODS_NUM=? " +
				"WHERE SHOPPINGCART_ID=?";
		
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, shoppingcart.getGoodsNum());
			ps.setLong(2, shoppingcart.getShoppingcartId());
			int i  =ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	@Override
	public List<Shoppingcart> getAllListPage(int currentPage, int pageSize,Long id){
		// TODO Auto-generated method stub
		List<Shoppingcart> list = new ArrayList<Shoppingcart>();
		String sql = "SELECT * FROM SHOPPINGCART S "
				+ " LEFT JOIN GOODS G ON S.GOODS_ID=G.GOODS_ID left join user se on se.user_id=g.user_id "
				+ " LEFT JOIN USER U ON S.USER_ID=U.USER_ID WHERE 1=1";
		if(id!=null&&id!=0){
			sql = sql+" AND S.user_ID="+id;
		}
		sql = sql +" LIMIT ?,?";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (currentPage-1)*pageSize);
			ps.setInt(2, pageSize);
			rs =ps.executeQuery();
			while(rs.next()){
				Shoppingcart s = new Shoppingcart();
				s.setShoppingcartId(rs.getLong("SHOPPINGCART_ID"));
				s.setGoodsNum(rs.getLong("GOODS_NUM"));
				User user = new User();
				user.setUserId(rs.getLong("USER_ID"));
				s.setUser(user);
				Goods goods = new Goods();
				goods.setGoodsId(rs.getLong("GOODS_ID"));
				goods.setGoodsName(rs.getString("goods_name"));
				goods.setGoodsPrice(rs.getDouble("goods_price"));
				goods.setGoodsAccount(rs.getLong("goods_account"));
				goods.setGoodsShelves(rs.getLong("goods_shelves"));
				User user2=new User();
				user2.setUserShopname(rs.getString("user_shopname"));
				goods.setUser(user2);
				s.setGoods(goods);
				list.add(s);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return list;
	}

	@Override
	public int getTotalCount() {
		// TODO Auto-generated method stub
		String sql = "SELECT COUNT(*) FROM SHOPPINGCART ";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs =ps.executeQuery();
			if(rs.next()){
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return 0;
	}

	@Override
	public String getUserShopnameByGoodsId(Long goodsid) {
		// TODO Auto-generated method stub
		String sql = "SELECT * FROM SHOPPINGCART S "
				+ "LEFT JOIN USER U ON S.USER_ID=U.USER_ID "
				+ "LEFT JOIN GOODS G ON S.GOODS_ID=G.GOODS_ID WHERE S.GOODS_ID=?";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs =ps.executeQuery();
			if(rs.next()){
				return rs.getString("user_shopname");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return "";
	}

	@Override
	public boolean clearShoppingcart(Long id) {
		String sql = "DELETE FROM SHOPPINGCART WHERE user_ID=?";
		
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, id);
			int i  =ps.executeUpdate();
			if(i>0){
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

}
